﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ImageProcessing.Utilities;
using ImageProcessing.IntermediateClasses;

namespace ImageProcessing
{
    public partial class Morphology : Form
    {
        private Image originalImage;
        int[,] matrixValues;
        public Morphology(Image originalImage)
        {
            InitializeComponent();
            this.originalImage = originalImage;
            this.picBoxOriginal.Image = this.originalImage;
            this.matrixValues = new int[3, 3];
            this.CreateDataGridMatrix(3,3);
        }

        private void CreateDataGridMatrix(int width, int height)
        {
            try
            {
                DataTable table = new DataTable();
                for (int i = 0; i < width; i++)
                {
                    table.Columns.Add((i + 1).ToString());
                }
                for (int i = 0; i < height; i++)
                {

                    table.Rows.Add(table.NewRow());
                }
                this.dataGridMatrix.DataSource = table;
            }
            catch (Exception ex)
            {
                Logger.LogException(ex);
                throw;
            }
        }

        private void btnDilation_Click(object sender, EventArgs e)
        {
            matrixValues = new int[int.Parse(this.txtHeight.Text), int.Parse(this.txtWidth.Text)];
            for (int i = 0; i < int.Parse(this.txtHeight.Text); i++)
            {
                for (int j = 0; j < int.Parse(this.txtWidth.Text); j++)
                {
                    matrixValues[i, j] = int.Parse(this.dataGridMatrix.Rows[i].Cells[j].Value.ToString());
                }
            }
            Bitmap temp = new Bitmap(this.picBoxOriginal.Image);
            ImageProcessing.IntermediateClasses.Morphology morph = new IntermediateClasses.Morphology(temp, this.matrixValues);
            morph.MaskHeight = int.Parse(this.txtHeight.Text);
            morph.MaskWidth = int.Parse(this.txtWidth.Text);
            Noise.Index tempIndex = new Noise.Index();
            tempIndex.X = int.Parse(this.txtX.Text);
            tempIndex.Y = int.Parse(this.txtY.Text);
            morph.Origin = tempIndex;
            this.picBoxModified.Image = morph.Dilation();
        }

        private void btnErosion_Click(object sender, EventArgs e)
        {
            matrixValues = new int[int.Parse(this.txtHeight.Text), int.Parse(this.txtWidth.Text)];
            for (int i = 0; i < int.Parse(this.txtHeight.Text); i++)
            {
                for (int j = 0; j < int.Parse(this.txtWidth.Text); j++)
                {
                    matrixValues[i, j] = int.Parse(this.dataGridMatrix.Rows[i].Cells[j].Value.ToString());
                }
            }
            Bitmap temp = new Bitmap(this.picBoxOriginal.Image);
            ImageProcessing.IntermediateClasses.Morphology morph = new IntermediateClasses.Morphology(temp, this.matrixValues);
            morph.MaskHeight = int.Parse(this.txtHeight.Text);
            morph.MaskWidth = int.Parse(this.txtWidth.Text);
            Noise.Index tempIndex = new Noise.Index();
            tempIndex.X = int.Parse(this.txtX.Text);
            tempIndex.Y = int.Parse(this.txtY.Text);
            morph.Origin = tempIndex;
            this.picBoxModified.Image = morph.Erosion();
        }

        private void txtWidth_TextChanged(object sender, EventArgs e)
        {
            DataTable table = new DataTable();
            for (int i = 0; i < int.Parse(this.txtWidth.Text); i++)
            {
                table.Columns.Add((i + 1).ToString());

            }
            this.dataGridMatrix.DataSource = table;
        }

        private void txtHeight_TextChanged(object sender, EventArgs e)
        {
            DataTable table = new DataTable();
            for (int i = 0; i < int.Parse(this.txtWidth.Text); i++)
            {
                table.Columns.Add((i + 1).ToString());

            }
            for (int i = 0; i < int.Parse(this.txtHeight.Text); i++)
            {
                table.Rows.Add(table.NewRow());

            }
            this.dataGridMatrix.DataSource = table;
        }


    }
}
